package com.ruoyi.cngj.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cngj.domain.CngjNews;
import com.ruoyi.cngj.service.ICngjNewsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 新闻动态Controller
 * 
 * @author yuexl
 * @date 2024-06-27
 */
@Controller
@RequestMapping("/cngj/news")
public class CngjNewsController extends BaseController
{
    private String prefix = "cngj/news";

    @Autowired
    private ICngjNewsService cngjNewsService;

    //@RequiresPermissions("cngj:news:view")
    @GetMapping()
    public String news()
    {
        return prefix + "/news";
    }

    /**
     * 查询新闻动态列表
     */
    //@RequiresPermissions("cngj:news:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CngjNews cngjNews)
    {
        startPage();
        List<CngjNews> list = cngjNewsService.selectCngjNewsList(cngjNews);
        return getDataTable(list);
    }

    /**
     * 导出新闻动态列表
     */
    //@RequiresPermissions("cngj:news:export")
    @Log(title = "新闻动态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CngjNews cngjNews)
    {
        List<CngjNews> list = cngjNewsService.selectCngjNewsList(cngjNews);
        ExcelUtil<CngjNews> util = new ExcelUtil<CngjNews>(CngjNews.class);
        return util.exportExcel(list, "新闻动态数据");
    }

    /**
     * 新增新闻动态
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存新闻动态
     */
    //@RequiresPermissions("cngj:news:add")
    @Log(title = "新闻动态", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CngjNews cngjNews)
    {
        return toAjax(cngjNewsService.insertCngjNews(cngjNews));
    }

    /**
     * 修改新闻动态
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CngjNews cngjNews = cngjNewsService.selectCngjNewsById(id);
        mmap.put("cngjNews", cngjNews);
        return prefix + "/edit";
    }

    /**
     * 修改保存新闻动态
     */
    //@RequiresPermissions("cngj:news:edit")
    @Log(title = "新闻动态", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CngjNews cngjNews)
    {
        return toAjax(cngjNewsService.updateCngjNews(cngjNews));
    }

    /**
     * 删除新闻动态
     */
    //@RequiresPermissions("cngj:news:remove")
    @Log(title = "新闻动态", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cngjNewsService.deleteCngjNewsByIds(ids));
    }

    /**
     * 改变位置
     */
    @Transactional
    @PostMapping("/newsMove")
    @ResponseBody
    public AjaxResult newsMove(String type,String id,String changeId)
    {
        CngjNews cngjNews = cngjNewsService.selectCngjNewsById(id);
        CngjNews changeCngiNews = cngjNewsService.selectCngjNewsById(changeId);
        int num =0;
        if("1".equals(type) || "2".equals(type)){
            //上移
            int currSort = cngjNews.getNewsSort();
            cngjNews.setNewsSort(changeCngiNews.getNewsSort());
            changeCngiNews.setNewsSort(currSort);
            num +=cngjNewsService.updateCngjNews(cngjNews);
            num +=cngjNewsService.updateCngjNews(changeCngiNews);
        }
        if("3".equals(type)){
            cngjNews.setNewsSort(Integer.parseInt(changeId)+1);
            num +=cngjNewsService.updateCngjNews(cngjNews);
        }
        return toAjax(num);
    }

    /**
     * 启用停用banner素材
     */
    @Log(title = "启用停用banner素材")
    @PostMapping( "/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(String id,String newsState)
    {
        CngjNews cngjNews = new CngjNews();
        cngjNews.setId(id);
        cngjNews.setNewsState(newsState);
        return toAjax(cngjNewsService.updateCngjNews(cngjNews));
    }

}
